package com.burgeon.backend.Util;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

public class CodeImageUtil {


    public static void mergeImage(String bigPath,String smallPath,String x,String y) throws IOException {

        try {
            BufferedImage small ;
            BufferedImage big = ImageIO.read(new File(bigPath));
            if(smallPath.contains("http")){

                URL url = new URL(smallPath);
                small = ImageIO.read(url);
            }else{
                small = ImageIO.read(new File(smallPath));
            }

            Graphics2D g = big.createGraphics();

            float fx = Float.parseFloat(x);
            float fy = Float.parseFloat(y);
            int x_i = (int)fx;
            int y_i = (int)fy;
            g.drawImage(small, x_i, y_i,small.getWidth(),small.getHeight(), null);
            g.dispose();
            ImageIO.write(big, "png", new File(bigPath));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }





    //生成二维码后，需要将二维码嵌入另一张图片中，实现比较简单，只需要在背景图片上画上已经生成的二维码即可
    public static String changeMerchantSeatQrcodeImage(BufferedImage zxingImage, String backgroundPath) {
        InputStream imagein = null;
        ImageOutputStream imOut = null;
        OutputStream outputStream = null;
        try {
            imagein = new FileInputStream(backgroundPath);
            BufferedImage image = ImageIO.read(imagein);
            BufferedImage image2 = zxingImage;
            Graphics g = image.getGraphics();
            // 生成的二维码设置的较小，这里等比放大了二维码。也可在zxing中设置二维码生成的大小
            BufferedImage squreImage = resizeImage(image2, 2);
            g.drawImage(squreImage, 40, 25,
                    squreImage.getWidth(), squreImage.getHeight(), null);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();

            imOut = ImageIO.createImageOutputStream(bs);
            ImageIO.write(image, "jpg", imOut);
            InputStream is = new ByteArrayInputStream(bs.toByteArray());

            // 上传后的路径code
            String filePath = "D:/code/img/hebing-WaMaterial/" + 1 + ".jpg";
            //String furl = "http://lfwd.cloudsmicro.com/img/" + account + "-Wamaterial/" + id + "/.png";
            File file = new File(filePath);
            if (!file.exists()){
                file.getParentFile().mkdirs();
                // file.createNewFile();
            }
            outputStream = new FileOutputStream(file);
            int len = 0;
            byte[] buf = new byte[1024];
            while ((len = is.read(buf, 0, 1024)) != -1) {
                outputStream.write(buf, 0, len);
            }
            outputStream.flush();//http://lfwd.cloudsmicro.com
            String furl = "http://lfwd.cloudsmicro.com/img/" + "hebing" + "-Wamaterial/" + 1 + ".jpg";

           // return is;
            return furl;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                imagein.close();
                imOut.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


//放大图片方法：
    public static BufferedImage resizeImage(BufferedImage  originalImage, double times){
        int width = (int)(originalImage.getWidth()/times);
        int height = (int)(originalImage.getHeight()/times);

        int tType = originalImage.getType();
        if(0 == tType){
            tType = 5;
        }
        BufferedImage newImage = new BufferedImage(width,height, tType);
        Graphics g = newImage.getGraphics();
        g.drawImage(originalImage, 0,0,width,height,null);
        g.dispose();

        return newImage;
    }


}
